package com.lxq.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;

import com.lxq.common.DbHelper;
import com.lxq.dao.OrderRechargeDao;
import com.lxq.dao.impl.common.BaseDaoImpl;
import com.lxq.entity.OrderRecharge;

public class OrderRechargeDaoImpl extends BaseDaoImpl<OrderRecharge> implements OrderRechargeDao {

	@Override
	public boolean updateOrderState(String orderId, int orderState) {
		if(findById(orderId) == null){
			return false;
		}
		
		Connection conn = null;
		Date date = new Date(Calendar.getInstance().getTimeInMillis());
		Object[] params = { orderState, date, orderId };

		String sql = "update " + getTableName() + " set orderState=? ,orderTime=? where orderId=?";
		try {

			conn = DbHelper.getConn();
			qr.update(conn , sql, params);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				DbHelper.close(conn);
			}
		}
		return true;
	}

	@Override
	public List<OrderRecharge> findOrder(String phoneNumber) {
		List<OrderRecharge> orders = null;

		Connection conn = null;
		String sql = "select * from " + getTableName() + " where phoneNumber=?";

		try {

			conn = DbHelper.getConn();
			orders = qr.query(conn, sql, beanListHandler, phoneNumber);

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			if (conn != null) {
				DbHelper.close(conn);
			}
		}

		return orders;
	}

	@Override
	public List<OrderRecharge> findOrder(String phoneNumber, String cardNumber) {
		List<OrderRecharge> orders = null;

		Connection conn = null;
		String sql = "select * from " + getTableName() + " where phoneNumber=? and cardNumber=?";

		try {

			conn = DbHelper.getConn();
			orders = qr.query(conn, sql, beanListHandler, phoneNumber, cardNumber);

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			if (conn != null) {
				DbHelper.close(conn);
			}
		}

		return orders;
	}

	@Override
	public OrderRecharge findById(String orderId) {
		OrderRecharge order = null;
		Connection conn = null;
		String sql = "select * from " + getTableName() + " where orderId =?";
		try {

			conn = DbHelper.getConn();
			order = qr.query(conn, sql, beanHandler, orderId);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				DbHelper.close(conn);
			}
		}
		return order;
	}

}
